package www.cskaoyan.com.controller;

import www.cskaoyan.com.bean.ClassManager;
import www.cskaoyan.com.service.ClassManagerService;
import www.cskaoyan.com.service.impl.ClassManagerServiceImpl;
import www.cskaoyan.com.utils.Request;
import www.cskaoyan.com.utils.Response;

import java.sql.SQLException;
import java.util.List;

public class ClassManagerController {

    // 调用逻辑层
    ClassManagerService classManagerService;
    public ClassManagerController(){

        this.classManagerService = new ClassManagerServiceImpl();
    }


    /**
     * 创建班级
     * @param request:  Request{url='ClassManager/create', methods='POST', parameters={beginDate=2023-02-26T1, className=一班}}
     *  @param request: parameters={beginDate=2023-2-27, className=一班}
     * @return Response(200, "成功", true)
     */
    public Response create(Request request)  {
        System.out.println("/ClassManager/create");
        System.out.println(request);
        Response response;
        ClassManager classManager = new ClassManager(request.getParameters());
        Integer classId = null;
        try {
             classManagerService.create(classManager);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response = new Response(200, "创建成功", true);
        return  response;
    }


    /**
     * 获取班级列表
     * @param request:
     *  @param request:
     * @return Response(200, "成功", true)
     */
    public Response getClassList(Request request)  {
        System.out.println("/ClassManager/getClassList");
        System.out.println(request);
        Response response;
        List<ClassManager> list = null;
        try {
           list =  classManagerService.getClassList();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list != null  && list.size() > 0){
            // 存在
            response = new Response(200, "班级列表", true);
            response.setData(list);
        }else {
            // 不存在
            response = new Response(0, "没有班级列表", true);
        }
        return  response;
    }

    /**
     * 删除班级
     * @param request:  Request{url='ClassManager/deleteClass', methods='Get', parameters={id=2}}
     *  @param request:  parameters={id=2}
     * @return Response(200, "成功", true)
     */
    public Response deleteClass(Request request)  {
        System.out.println("/ClassManager/deleteClass");
        System.out.println(request);
        Response response;
        ClassManager classManager = new ClassManager(request.getParameters());
        try {
            classManagerService.deleteClass(classManager);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response = new Response(200, "删除成功", true);
        return  response;
    }

}
